package com.ph.irp.base.log.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ph.irp.base.log.dao.PBaseLogDAO;
import com.ph.irp.base.log.service.LogService;
import com.ph.irp.base.usermanage.dao.PBasePurviewFunctionDAO;
import com.ph.irp.base.usermanage.dao.PBasePurviewFunctiontypeDAO;
import com.ph.irp.base.usermanage.dao.PBasePurviewUserDAO;
import com.ph.irp.dto.LogDTO;
import com.ph.irp.model.PBaseLog;
import com.ph.irp.model.PBasePurviewFunction;
import com.ph.util.DateUtil;

public class LogServiceImpl implements LogService {
	PBaseLogDAO logDaoProxy;
	PBasePurviewFunctionDAO functionDaoProxy;
	PBasePurviewFunctiontypeDAO functionTypeDaoProxy;
	PBasePurviewUserDAO userDaoProxy;

	public void setLogDaoProxy(PBaseLogDAO logDaoProxy) {
		this.logDaoProxy = logDaoProxy;
	}

	public void setFunctionDaoProxy(PBasePurviewFunctionDAO functionDaoProxy) {
		this.functionDaoProxy = functionDaoProxy;
	}

	public void setFunctionTypeDaoProxy(
			PBasePurviewFunctiontypeDAO functionTypeDaoProxy) {
		this.functionTypeDaoProxy = functionTypeDaoProxy;
	}

	public void setUserDaoProxy(PBasePurviewUserDAO userDaoProxy) {
		this.userDaoProxy = userDaoProxy;
	}

	public List<LogDTO> getAllLog() throws Throwable {

		return convert2DTO(logDaoProxy.getAllLog());

	}

	public List<LogDTO> getLogByUserNameAndTime(String customerName,
			String timeFrom, String timeTo) throws Throwable {
		List<PBaseLog> logList = new ArrayList<PBaseLog>();
		int userId = 0;
		if (customerName != null && !customerName.equals("")) {
			if(userDaoProxy.getUserListByUserName(customerName).size()!=0){
				userId = userDaoProxy.getUserListByUserName(customerName).get(0)
				.getUserId();
			}else{
				return convert2DTO(logList);
			}
			
		}
		if (timeFrom != null && !timeFrom.equals("") && timeTo != null
				&& !timeTo.equals("")) {
			timeFrom += DateUtil.STARTCOLOCK;
			timeTo += DateUtil.ENDCOLOCK;
		}

		if (customerName != null && customerName.equals("") && timeFrom != null
				&& timeFrom.equals("") && timeTo != null && timeTo.equals("")) {
			logList = logDaoProxy.getAllLog();
		} else {
			logList = logDaoProxy.getLogByUserNameAndTime(userId, timeFrom,
					timeTo);
		}

		return convert2DTO(logList);
	}

	private List<LogDTO> convert2DTO(List<PBaseLog> logList) throws Throwable {
		List<LogDTO> logDTOList = new ArrayList<LogDTO>();
		for (PBaseLog log : logList) {
			LogDTO logDTO = new LogDTO();

			logDTO.setLogDate(DateUtil.time2String(log.getHappenTime(),
					"yyyy-MM-dd HH:mm:ss"));
			logDTO.setLogInfo(log.getLogInfo());

			PBasePurviewFunction function = functionDaoProxy
					.getFunctionByFunctionID(log.getFunctionId());
			
			logDTO.setState(getState2String(log.getState()));
			logDTO.setModuleName(functionTypeDaoProxy
					.getFunctionTypeByFunctionTypeID(function
							.getPBasePurviewFunctiontypeId())+"-"+function.getFunctionName());

			logDTO.setUserName(userDaoProxy.getUserByUserId(
					String.valueOf(log.getUserId())).getUserName());
			logDTOList.add(logDTO);
		}
		return logDTOList;

	}

	private String getState2String(int stateInt) {
		String state = null;
		// 1为开始，2为完成，3为取消，4为失败。缺省为0，无意义
		if (stateInt == 1) {
			state = "开始";
		}

		if (stateInt == 2) {
			state = "完成";
		}

		if (stateInt == 3) {
			state = "取消";
		}

		if (stateInt == 4) {
			state = "失败";
		}
		return state;
	}

	public void deleteAllLog() throws Throwable {
		logDaoProxy.deleteAllLog();
	}

	public void addLog(PBaseLog log) throws Throwable {
		logDaoProxy.addLog(log);
	}
}
